#include<stdlib.h>
#include<stdio.h>

#include"trace.h"
#include"compile.h"
#include"hosts.h"

/* get basename of a file */
const char * dbd_get_basename(const char * file_name){
    char *slash;

    if (!file_name)
        return file_name;
             
    slash = strrchr(file_name, '/');
                             
    if (slash == NULL || slash[1] == '\0')
        return file_name;

    return slash+1;
}

int dbd_get_compiler(char ** argv, char ***compile_args){
    //get total length of argv
}

int dbd_argv_len(char ** in){
    int len = 0;
    char **a;
    for(a=in;*a!=NULL;a++){
        len += strlen(*a)+1;    // +1 for space 
    }
    return len+1;
}

int dbd_copy_argv(char * to, char ** from){
    char **a;
    int ret;
    for(a=from;*a!=NULL;a++){
        ret = strcpy(to, *a);
        to += strlen(*a);
		to[0] = ' ';
		to ++;
    }
    return 1;
}

int main(int argc, char** argv){
    
    int ret,len;
    char *compiler_name;
    char *compiler_line;

    compiler_name= (char *)dbd_get_basename(argv[0]);
    
    if(strstr(compiler_name, "distbuild") == NULL 
		&& strstr(compiler_name, "a.out") == NULL){
		fprintf(stderr, "error %s\n", compiler_name);
        return 0;
    }
    
    if(argv <=1 ) return 1;

    //dbd_get_compiler(argv, compiler_args);
	dbd_empty_state();
    len = dbd_argv_len(argv+1);
    compiler_line = (char *)malloc( len * sizeof(argv[0][0]));

    ret = dbd_copy_argv(compiler_line, argv+1);
    ret = dbd_do_compile_timed( compiler_line );
    
}

